package com.aidex.biz.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.aidex.biz.domain.*;
import com.aidex.biz.mapper.*;
import com.aidex.biz.service.DaibanIngService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.uuid.UUID;
import com.aidex.system.mapper.SysDeptMapper;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.service.MonthDeptService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 月报管理Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-01-20
 */
@Service
@Transactional(readOnly = true)
public class MonthDeptServiceImpl extends BaseServiceImpl<MonthDeptMapper, MonthDept> implements MonthDeptService
{
    private static final Logger log = LoggerFactory.getLogger(MonthDeptServiceImpl.class);

    @Autowired
    private MonthModelMapper  monthModelMapper;
    @Autowired
    private MonthModelDetialsMapper monthModelDetialsMapper;

    @Autowired
    private MonthDeptDetialsMapper monthDeptDetialsMapper;

    @Autowired
    private MonthDeptListMapper monthDeptListMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Autowired
    private DaibanIngService daibanIngService;

    /**
     * 获取单条数据
     * @param monthDept 月报管理
     * @return 月报管理
     */
    @Override
    public MonthDept get(MonthDept monthDept)
    {

        MonthDept dto = super.get(monthDept);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 月报管理id
     * @return 月报管理
     */
    @Override
    public MonthDept get(String id)
    {
        MonthDept dto = super.get(id);
        return dto;
    }


    /**
     * 查询月报管理列表
     * @param monthDept 月报管理
     * @return 月报管理
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<MonthDept> findList(MonthDept monthDept)
    {
        return super.findList(monthDept);
    }

    /**
     * 分页查询月报管理列表
     * @param monthDept 月报管理
     * @return 月报管理
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<MonthDept> findPage(MonthDept monthDept)
    {
        return super.findPage(monthDept);
    }

    /**
     * 保存月报管理
     * @param monthDept
     * @return 结果
     */
    @Override
    public boolean save(MonthDept monthDept) {
        return super.save(monthDept);
    }


    /**
     * 删除月报管理信息
     * @param monthDept
     * @return 结果
     */
    @Override
    public boolean remove(MonthDept monthDept)
    {
        return super.remove(monthDept);
    }

    /**
     * 批量删除月报管理
     * @param ids 需要删除的月报管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteMonthDeptByIds(String[] ids)
    {
        return mapper.deleteMonthDeptByIds(ids);
    }

    /**
     * 获取最大编号
     * @param monthDept 月报管理
     * @return 结果
     */
    @Override
    public int findMaxSort(MonthDept monthDept)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(monthDept));
    }

    @Override
    @Transactional(readOnly = false)
    public boolean batchSave(MonthDept monthDept) {
        try{
            SysDept sysDept=new SysDept();
            sysDept.setTreeLevel(2);
            List<SysDept> sysDeptList=sysDeptMapper.findList(sysDept);
            MonthModel monthModel=monthModelMapper.get(monthDept.getModelId());
            List<DaibanIng> daibanIngs=new ArrayList<>();
            for (SysDept dept:sysDeptList) {
                String listId = UUID.randomUUID().toString();
                MonthDeptList monthDeptList=new MonthDeptList();
                monthDeptList.setModelId(monthDept.getModelId());
                monthDeptList.setDeptId(dept.getId());
                monthDeptList.setDeptName(dept.getDeptName());
                monthDeptList.setUserId(SecurityUtils.getUserId());
                monthDeptList.setUserName(SecurityUtils.getLoginUser().getUser().getName());
                monthDeptList.setName(monthModel.getName());
                monthDeptList.setTimes(monthDept.getTimes());
                monthDeptList.setUpdateBy(SecurityUtils.getLoginUser().getUser().getName());
                monthDeptList.setUpdateTime(new Date());
                monthDeptList.setCreateTime(new Date());
                monthDeptList.setId(listId);
                monthDeptListMapper.insert(monthDeptList);
                DaibanIng daibanIng=new DaibanIng();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                daibanIng.setContent("("+sdf.format(monthDept.getTimes())+"年)"+"月报");
                daibanIng.setType("年度月报");
                daibanIng.setDeptId(monthDept.getDeptId());
                daibanIng.setRouter("baobiao/bizMonthDeptList");
                daibanIng.setPath("biz/bizassesstodept/myAssessInfo");
                daibanIng.setStatus("0");
                daibanIng.setDeptId(dept.getId());
                daibanIngs.add(daibanIng);
                for (int i = 1; i <= 12; i++) {
                    MonthDept dto = new MonthDept();
                    String id = UUID.randomUUID().toString();
                    dto.setId(id);
                    dto.setDeptId(dept.getId());
                    dto.setDeptName(dept.getDeptName());
                    dto.setModelId(listId);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
                    String month = i < 10 ? "0" + i : i + "";
                    SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy-MM");
                    dto.setTimes(simpleDateFormats.parse(simpleDateFormat.format(monthDept.getTimes()) + "-" + month));
                    dto.setName(monthModel.getName());
                    dto.setStatus("0");
                    dto.setSort((long) i);
                    dto.setIsNewRecord();
                    super.save(dto);
                    MonthModelDetials monthModelDetials = new MonthModelDetials();
                    monthModelDetials.setMonthModelId(monthDept.getModelId());
                    List<MonthModelDetials> monthModelDetialsList = monthModelDetialsMapper.findList(monthModelDetials);
                    int count=0;
                    for (MonthModelDetials monthModelDetial : monthModelDetialsList) {
                        MonthDeptDetials monthDeptDetials = new MonthDeptDetials();
                        monthDeptDetials.setMonthDeptId(id);
                        monthDeptDetials.setLeimu(monthModelDetial.getLeimu());
                        monthDeptDetials.setProjects(monthModelDetial.getProjects());
                        monthDeptDetials.setStatus("0");
                        monthDeptDetials.setRemark(monthModelDetial.getRemark());
                        monthDeptDetials.setId(UUID.randomUUID().toString());
                        monthDeptDetials.setSort((long) count);
                        monthDeptDetials.setCreateTime(new Date());
                        monthDeptDetialsMapper.insert(monthDeptDetials);
                        count++;
                    }
                }

            }
            daibanIngService.insertBatch(daibanIngs);
        }catch (Exception e)
        {
            e.printStackTrace();
            return  false;
        }
        return true;
    }

    @Override
    public List<MonthDept> findListOrderBy(MonthDept monthDept) {
        return mapper.findListOrderBy(monthDept);
    }
}
